package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("adminCategoryController")
@Slf4j
@RequestMapping("/admin/category")
@Api(tags="类型分类")
public class CategoryController {

    @Autowired
    CategoryService categoryService;
    /**
     * 对分类进行分页查询
     * @return
     */
    @ApiOperation("对分类进行分页查询")
    @GetMapping("/page")
    public Result<PageResult>selectPage(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询:{}",categoryPageQueryDTO);
        PageResult pageResult=categoryService.selectPage(categoryPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 更新分类
     * @return
     */
    @ApiOperation("更新分类信息")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("更新分类信息:{}",categoryDTO);
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }


    /**
     * 启用或禁用分类
     * @return
     */
    @ApiOperation("启用禁用分类")
    @PostMapping("/status/{status}")
    public Result startOrStopCategory(@PathVariable("status") Integer status,Long id){
        if (status== StatusConstant.DISABLE) log.info("禁用分类:{}",id);
        else log.info("启用分类:{}",id);
        categoryService.startOrStopCategory(id,status);
        return Result.success();
    }


    /**
     * 根据类型查看分类
     * @return
     */
    @ApiOperation("根据类型查看分类")
    @GetMapping("/list")
    public Result<List<Category>>selectCategoryByType(Integer type){
        log.info("根据分类类型{}查看分类",type);

        List<Category>categories=categoryService.selectCategoryByType(type);

        return Result.success(categories);
    }


    /**
     * 新增分类
     * @return
     */
    @ApiOperation("新增分类")
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类:{}",categoryDTO);
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }


    /**
     * 根据id删除分类
     * @return
     */
    @ApiOperation("根据id删除分类")
    @DeleteMapping
    public Result deleteCategoryById(Long id){
        log.info("即将删除分类{}",id);
        categoryService.deleteCategoryById(id);
        return Result.success();
    }

}
